package com.zc.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zc.edu.entity.Comment;
import com.zc.edu.service.CommentService;
import com.zc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author cong
 * @since 2022-12-04
 */
@RestController
@RequestMapping("/eduService/comment")
@CrossOrigin
public class CommentController {
	@Autowired
	private CommentService commentService;

	// 根据课程id分页查询评论
    @GetMapping("/getCommentsByCourseId/{courseId}/{currentPage}/{size}")
	public R getCommentsByCourseId(@PathVariable String courseId,
                                   @PathVariable long currentPage,
                                   @PathVariable long size) {

	    Page<Comment> commentPage = new Page<>(currentPage, size);
	    QueryWrapper<Comment> wrapper = new QueryWrapper<>();
	    wrapper.eq("course_id", courseId);
	    Map<String, Object> map = commentService.pageComment(commentPage, wrapper);
	    return R.ok().data(map);
    }


    // 添加评论
	@PostMapping("/addComment")
	public R addComment(@RequestBody Comment comment) {
		boolean save = commentService.save(comment);
		return save ? R.ok() : R.error();
	}

}

